package com.xwdeshj.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xwdeshj.dal.Dal_Xwfarmcollect;
import com.xwdeshj.entity.XwFarmCollectinfo;
import com.xwdeshj.entity.XwFarmDiscussinfo;
import com.xwdeshj.repository.XwFarmCollectinfoRepository;
import com.xwdeshj.service.XwFarmCollectinfoService;
import com.xwdeshj.utils.helper;
import lombok.extern.slf4j.Slf4j;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by BKWJA on 2019/1/6.
 * 农家乐——农家收藏信息 实现类
 */
@Slf4j
@Service("xwFarmCollectinfoService")
public class XwFarmCollectinfoServiceImpl implements XwFarmCollectinfoService {

    @Autowired
    private XwFarmCollectinfoRepository xwFarmCollectinfoRepository;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private Sid sid;

    Dal_Xwfarmcollect dal_xwfarmcollect = new Dal_Xwfarmcollect();

    /**
     *  获取农家乐店铺  收藏信息
     */
    @Override
    public JSONObject getfarmcollectlist(Integer pageindex, Integer pagesize, String starttime, String endtime,
                                         String sortfild, String sortway, String searchval, String searchkey,
                                         String farm_encryptId,String user_encryptId, HttpServletRequest request) {
        JSONObject result = new JSONObject();

        result = dal_xwfarmcollect.getfarmcollectlist(pageindex,pagesize,starttime,endtime,sortfild,sortway,
                searchval,searchkey,farm_encryptId,user_encryptId,jdbcTemplate);

        System.out.println("getfarmdiscusslist:"+result.toString());
        return result;
    }

    /**
     * 农家乐——取消收藏
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    public JSONObject delCollectfarm(Integer collect_id, HttpServletRequest request) {
        JSONObject result = new JSONObject();
        String uid = helper.ReadCookieMap(request,"uid").toString();//读取当前操作人用户id
        String u_encryptid = helper.ReadCookieMap(request,"uid_encryptid").toString();//读取当前操作人用户加密id
        if((!"".equals(uid) && uid != null) && (!"".equals(u_encryptid) && u_encryptid != null)){

            XwFarmCollectinfo xwFarmCollectinfo = new XwFarmCollectinfo();
            xwFarmCollectinfo.setFarmCollectId(collect_id);
            boolean temp = xwFarmCollectinfoRepository.exists(Example.of(xwFarmCollectinfo));
            if(temp){
                try {
                    xwFarmCollectinfoRepository.deleteByFarmCollectId(collect_id);
                    result.put("status",0);
                    result.put("msg","ok");
                }catch (Exception e){
                    e.printStackTrace();
                    result.put("status",40002);
                    result.put("msg",e);
                }

            }else {
                result.put("status",40002);
                result.put("msg","该记录不存在！");
            }

        }else {
            result.put("status",40001);
            result.put("msg","uid不能为空，请先登录后再操作！");
        }
        System.out.println("delCollectfarm:"+result.toString());
        return result;
    }


    public Dal_Xwfarmcollect getDal_xwfarmcollect() {
        return dal_xwfarmcollect;
    }

    public void setDal_xwfarmcollect(Dal_Xwfarmcollect dal_xwfarmcollect) {
        this.dal_xwfarmcollect = dal_xwfarmcollect;
    }
}
